package org.gangcai.mac.shop.fragment;

import android.graphics.drawable.AnimatedVectorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.activity.Animation1Activity;
import org.gangcai.mac.shop.activity.BillDetailActivity;
import org.gangcai.mac.shop.activity.OrderCompleteActivity;
import org.gangcai.mac.shop.activity.ShopReSaleActivity;
import org.gangcai.mac.shop.activity.WebActivity;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.HomeDataBean;
import org.gangcai.mac.shop.bean.OrderBean;
import org.gangcai.mac.shop.bean.PostsArticleBaseBean;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.manager.RetrofitManageres;
import org.gangcai.mac.shop.manager.RetrofitManagers;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.rxlife.RxFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class OrderListFragment extends RxFragment implements OnLoadmoreListener, OnRefreshListener {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.multiple_status_view)
    MultipleStatusView multipleStatusView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;

    private String mParam1;
    private String mParam2;
    private ArrayList<PostsArticleBaseBean> dataList;
    private CommonAdapter<PostsArticleBaseBean> adapter;
    /**
     * 默认是货源信息
     */
    private String type = "1";

    public OrderListFragment() {
        // Required empty public constructor
    }

    public static OrderListFragment newInstance(int param1, String param2) {
        OrderListFragment fragment = new OrderListFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = String.valueOf(getArguments().getInt(ARG_PARAM1));
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();

        refreshLayout.setOnRefreshListener(this);

        refreshLayout.setOnLoadmoreListener(this);
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.autoRefresh();
    }

    private void loadData() {
        multipleStatusView.showContent();
        RetrofitManageres.create().HomeData("Portal","list","indexJson",type)
                .compose(this.<HomeDataBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<HomeDataBean>() {
                    @Override
                    public void onSuccess(HomeDataBean value) {
                        refreshLayout.finishRefresh();
                        if (value.getCode() == Constant.CODE_SUCC) {

                            if (value.getReferer() == null || value.getReferer().getPosts().size() == 0) {
                                multipleStatusView.showEmpty();
                            } else {
                                multipleStatusView.showContent();

                                dataList.clear();
                                dataList.addAll(value.getReferer().getPosts());
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
                });
    }

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        dataList = new ArrayList<>();
        adapter = new CommonAdapter<PostsArticleBaseBean>(getContext(), R.layout.fragment_thinkcmf_recyclerview_item1, dataList) {
            @Override
            protected void convert(ViewHolder holder, final PostsArticleBaseBean bean, int position) {
                String titleString=bean.getPost_title();
                if(titleString.length()>24){
                    titleString = titleString.substring(0, 24);
                }
                holder.setText(R.id.animation1_title, titleString);
                holder.setText(R.id.animation1_text, bean.getPost_date());
                holder.setVisible(R.id.animation1_image,bean.getTid().equals("38"));
            }

        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener(){
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                PostsArticleBaseBean postsArticleBaseBean=dataList.get( position );
                Bundle bundle = new Bundle();
                String postID=postsArticleBaseBean.getObject_id();
                String cid=postsArticleBaseBean.getTerm_id();
                String webString="http://thinkcmf.500-china.com/index.php?g=&m=article&a=index&id="+postID+"&cid="+cid;
                bundle.putString("webString", webString);
                bundle.putString("title" , postsArticleBaseBean.getPost_title());
                bundle.putString("postID" , postID);
                ActivityUtils.startActivity(bundle, getActivity(), WebActivity.class);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        recyclerview.setAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        loadData();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        loadData();
    }
}
